home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3114 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: svnews.ubinet.ubs.com!ubszh!ubszh!jis
  2. From: jis@ubszh.net.ch (Johnston Ian (by ubsswop))
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual Members: Difficult Question
  5. Date: 22 Jan 1996 10:09:21 GMT
  6. Organization: UBS
  7. Distribution: world
  8. Message-ID: <4dvnoh$rbd@ubszh.fh.zh.ubs.com>
  9. References: <4dmha6$80c@marlin.ssnet.com> <4doof9$hom@charnel.ecst.csuchico.edu> <4doss3$b1i@marlin.ssnet.com>
  10. NNTP-Posting-Host: nol2179.fh.zh.ubs.com
  11.  
  12. In article <4doss3$b1i@marlin.ssnet.com>, helie@ssnet.com (Ray Helie) writes:
  13.  
  14. [...]
  15.  
  16. |> In a nutshell, I declared object of derived size, cast to the
  17. |> base, cast again to a parent class of base, cast back to base,
  18. |> tried to write a derived amount of data to the base class (as if
  19. |> it was an array of characters, starting at the object's address,
  20. |> which is the same way I wrote the information to begin with), and
  21. |> it dies on me when I do a virtual function call.  The derived amount of 
  22. |> space is there -- that's what I started with.  
  23. |> 
  24. |> So I don't think you understand my question, because I understand
  25. |> what you're saying, you are right under the circumstances you
  26. |> described, but I think you got the circumstances wrong...?
  27. |> 
  28. |> 
  29. |> If anyone else can help, I'd appreciate it.  :)
  30.  
  31. Well, it looks like you are overwriting the vtable ptr, and then when
  32. you call a virtual function you are going off to never-never land (because
  33. the pointer to the function in the vtable is corrupt).
  34.  
  35. Your downcast from "parent of base" to "base" could be causing a problem, if
  36. you have multiple inheritance.
  37.  
  38. I didn't see your original post, but you say you are "writing the
  39. information" starting "at the object's address". Let me guess: you are wriing
  40. the object out to disk or other external source, then trying to read it in?
  41.  
  42. If that's the case, you just can't do it (reliably). The innards of an
  43. object with virtual functions are more than just the object's data members;
  44. there is also a pointer to a table of virtual functions (and a pointer
  45. to base classes with virtual inheritance) [at least, this is how objects
  46. are usually implemented].
  47.  
  48. You just can't write out the pointer to a virtual function table to disk,
  49. read it aback, and expect to have things work. You wouldn't write the value
  50. of any other pointer out to disk, read it back, and expect it still to point
  51. to the same thing, would you?
  52.  
  53. Ian
  54.  
  55.